package com.habbot.beautifyinghandbook;

import java.util.ArrayList;
import java.util.List;

import com.habbot.beautifyinghandbook.adapters.MainListAdapter;
import com.habbot.beautifyinghandbook.helpers.GlobalVariables;
import com.habbot.beautifyinghandbook.models.MenuTable;
import com.habbot.beautifyinghandbook.objects.MenuObject;
import com.habbot.beautifyinghandbook.ui.InfoDialog;

import android.content.Intent;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.ProgressBar;

public class MainActivity extends BaseActivity implements OnItemClickListener {
	private ListView mListView;
	private ProgressBar mProgressBar;
	private ArrayList<MenuObject> mMenu;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initComponents();
		initAdView(R.id.ad);
		addListeners();
		new LoadMenuItemTask().execute();
	}

	@Override
	void initComponents() {
		addNavigation(getString(R.string.activity_main_title),
				R.drawable.btn_info_selector, R.drawable.btn_search_selector);
		setNavigationBackground(R.drawable.ic_topbar1);
		setTitleColor(Color.parseColor("#cdff4f"));

		mProgressBar = (ProgressBar) findViewById(R.id.progressBar);
		mListView = (ListView) findViewById(R.id.list);

		mMenu = new ArrayList<MenuObject>();
		// Add favorite to menu
		MenuObject fav = new MenuObject();
		fav.setIconRes(R.drawable.ic_yeuthich);
		fav.setTitle(getString(R.string.favorite));
		mMenu.add(fav);
	}

	@Override
	void addListeners() {
		setNavIcLeftClickListener(this);
		setNavIcRightClickListener(this);
		mListView.setOnItemClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.nav_ic_left:
			new InfoDialog(MainActivity.this).show();
			break;
		case R.id.nav_ic_right:

			break;
		default:
			break;
		}

	}

	private class LoadMenuItemTask extends
			AsyncTask<Void, Void, List<MenuObject>> {
		private MenuTable menuTable = new MenuTable(MainActivity.this);

		@Override
		protected void onPreExecute() {
			mListView.setVisibility(View.INVISIBLE);
			mProgressBar.setVisibility(View.VISIBLE);
			super.onPreExecute();
		}

		@Override
		protected List<MenuObject> doInBackground(Void... params) {
			List<MenuObject> result = menuTable.getAllMenu();
			if (result == null)
				return null;
			mMenu.addAll(result);
			for (int i = 0; i < GlobalVariables.MENU_ICON.length; i++) {
				mMenu.get(i + 1).setIconRes(GlobalVariables.MENU_ICON[i]);
			}
			return mMenu;
		}

		@Override
		protected void onPostExecute(List<MenuObject> result) {
			if (result != null) {
				mListView.setAdapter(new MainListAdapter(MainActivity.this,
						R.layout.mainlist_item, mMenu));
				mListView.setVisibility(View.VISIBLE);
				mProgressBar.setVisibility(View.INVISIBLE);
			}
			super.onPostExecute(result);
		}

	}

	@Override
	public void onItemClick(AdapterView<?> adapterView, View v, int position,
			long id) {
		if (position == 0) {
			Intent intent = new Intent(MainActivity.this,
					FavouriteActivity.class);
			startActivity(intent);
			overridePendingTransition(R.anim.slide_in_right,
					R.anim.slide_out_left);
		} else {
			Intent intent = new Intent(MainActivity.this,
					ListItemActivity.class);
			intent.putExtra("MENU_ID", ((MenuObject) v.getTag()).getId());
			startActivity(intent);
		}

	}
}
